﻿namespace Hippo.Agent.Lib
{
    using System;
    using System.Collections.Specialized;
    using System.Diagnostics.Contracts;
    using Hippo.Common;

    public class AgentConfigurationProfile : IConfigurationProfile
    {
        public const string ConsoleEndpointAddressIdentifier = "consoleEndpointAddress";
        public const string AgentNameIdentifier = "agentName";

        private readonly NameValueCollection settings;

        public AgentConfigurationProfile()
            : this(new NameValueCollection())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AgentConfigurationProfile"/> class.
        /// </summary>
        /// <param name="settings">Use the AppSettings here.</param>
        public AgentConfigurationProfile(NameValueCollection settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            Contract.EndContractBlock();

            this.settings = settings;
        }


        /// <summary>
        /// Gets the address to the console endpoint. A direct address to the console will skip the slow discovery process
        /// </summary>
        /// <value>The console endpoint.</value>
        public string ConsoleEndpointAddress
        {
            get { return Get(ConsoleEndpointAddressIdentifier); }
            set { Set(ConsoleEndpointAddressIdentifier, value); }
        }

        /// <summary>
        /// Gets the name of the agent, or uses machine name as default.
        /// </summary>
        /// <value>The name of the agent.</value>
        public string AgentName
        {
            get { return Get(AgentNameIdentifier) ?? Environment.MachineName; }
        }

        public void Set(string name, string value)
        {
            settings.Set(name, value);
        }

        public string Get(string key)
        {
            return settings[key];
        }
    }
}
